<template>
  <div class="tableAdd1">
    <van-collapse v-model="activeNames1">
      <van-collapse-item :title="title" name="1">
        <div style="position: relative">
          <div style="display: flex; height: 50px; line-height: 50px">
            <div class="footerButton" v-if="ISBianji">
              <van-button plain type="info" size="small" @click="handleAddusers11(button1[0])">{{ button1[0] }}</van-button>
              <van-button plain type="info" size="small" @click="handleAddusers11(button1[1])">{{ button1[1] }}</van-button>
            </div>
          </div>
          <el-table :header-cell-style="{ background: '#f1f1f1', color: '#000' }" size="small" :data="JFDataAll" border style="width: 100%" @selection-change="handleSelectionChange111">
            <el-table-column type="selection" width="30"> </el-table-column>
            <el-table-column v-for="(it, in1) in contentData1" :key="in1" :prop="it.key" :label="it.label" align="center">
              <template slot-scope="scope">
                <!-- 处理枚举数据 -->
                <span v-if="it.key === 'RERECONNAISSANCE'">{{ scope.row.RERECONNAISSANCE == '5' ? '是' : '否' }}</span>
                <span v-else-if="it.key == 'SEX'">{{ scope.row.SEX == '3' ? '男' : '女' }}</span>
                <span v-else>{{ scope.row[it.key] }}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </van-collapse-item>
    </van-collapse>
    <van-action-sheet v-model="isShow111" :key="tableAdd1kes" :title="button1[0]" get-container="body" show-cancel-button @confirm="saveBtn11" @cancel="cancelBtn11">
      <van-form validate-first>
        <div style="padding: 10px; height: 90%">
          <inputSelect
            :isShowCity="isShowCity"
            :contentDataCity="contentDataCity"
            :contentData.sync="contentData"
            :IStitle="false"
            :JFData.sync="JFData"
            :title="'申请户主信息'"
            @getValues="getValues" />
        </div>
      </van-form>
      <div class="footer1" style="height: 8vh; display: flex; justify-content: space-around; align-items: center">
        <van-button style="width: 30vw" size="small" type="info" @click="hadleFinsh('完成')">完成</van-button>
        <van-button plain style="width: 30vw" size="small" type="info" @click="hadleFinsh('提交并继续添加')">提交并继续添加</van-button>
      </div>
    </van-action-sheet>
  </div>
</template>

<script>
// import selectPicker from '@/utils/selectPicker'
// import { FileServerProxy } from '@/utils/api'
import {
  QuerySignCallback,
  InsertDataView,
  PrepareCreateProcess,
  FileServerProxy,
  CreateProcess,
  PlatSendTask,
  getRegister1,
  getSystemCityCode,
  dataTransFile,
  JFDataList,
  debounce,
  dataTransFilePreview,
  getDatalistForTree,
  getDataEnumByGroupName,
  numGenerateGetNum,
  existHouse
} from '@/utils/api'
import inputSelect from '../public/inputSelect'
import { v4 as uuidv4 } from 'uuid'
import timeFormat from '@/utils/timeFormat.js'
import { GSA_Add, GSA_Search } from '@/common/commonApi'
export default {
  filters: {
    inUseName1: function (msg, data) {
      try {
        let name = ''
        data.forEach((item) => {
          if (item.citycode == msg) {
            name = item.cityname
          }
        })
        return name
      } catch (err) {
        console.log(err)
      }
    },
    inUseName: function (msg, data) {
      try {
        let name = ''
        data.forEach((item) => {
          if (item.DICID == msg) {
            name = item.DICNAME
          }
        })
        return name
      } catch (err) {
        console.log(err)
      }
    }
  },
  data() {
    return {
      isShow111: false,
      activeNames1: ['1'],
      multipleSelection111: [],
      isShowCity: true,
      JFDataAll: [],
      tableAdd1kes: 1,
      contentDataCity: [
        {
          label: '所属市',
          key: 'CITY', //绑定的字段
          ISinput: true, //true:输入框
          required: true, //true: 必填项
          readonly: true, //true:可以填写 false：不可以填写
          NYDARR: []
        },
        {
          label: '所属县(区)',
          key: 'COUNTY', //绑定的字段
          ISinput: true, //true:输入框
          required: true, //true: 必填项
          readonly: true, //true:可以填写 false：不可以填写
          NYDARR: []
        },
        {
          label: '所属镇',
          key: 'TOWN', //绑定的字段
          ISinput: true, //true:输入框
          required: true, //true: 必填项
          readonly: true, //true:可以填写 false：不可以填写
          NYDARR: []
        },
        {
          label: '所属村',
          key: 'VILLAGE', //绑定的字段
          ISinput: true, //true:输入框
          required: true, //true: 必填项
          readonly: true, //true:可以填写 false：不可以填写
          NYDARR: []
        }
      ],
      newArr: []
    }
  },
  mounted() {
    this.newArr = this.JFData
    this.getTableData()
  },
  created() {},
  methods: {
    async saveBtn11() {
      //   let uuid1 = uuidv4()
      //   const date = await GSA_Add('af4270e8-e122-41b8-971a-6e4e2eaa5760', true)
    },
    getValues(name, value, label) {
      console.log(name, value, label, this.JFData)
    },
    cancelBtn11() {},
    handleSelectionChange111(val) {
      this.multipleSelection111 = val
      console.log(this.multipleSelection111, 'this.multipleSelection111')
    },
    setValue(name, val, label, key) {
      /**
       * name: 需要展示的name
       *  val： 选择的id值
       * lable ： 当前输入框的lable
       * key ： 需要赋值的key
       */
      this.JFData[0][key] = val
      this.$emit('update:JFData', this.JFData)
      this.$emit('getValues', name, val, label, key)
    },
    setValue1(name, val, label, key) {
      /**
       * name: 需要展示的name
       *  val： 选择的id值
       * lable ： 当前输入框的lable
       * key ： 需要赋值的key
       */
      this.JFData[0][key] = val
      this.$emit('update:JFData', this.JFData)
      this.$emit('getValues', name, val, label, key)
    },
    /**
     * 获取流水号
     */
    async getLSH(data) {
      //获取流水号之前先判断当前身份证号正不正确属不属于惠州市的 不是惠州市的要提示
      let date = new Date()
      this.JFData[0].CREATIONTIME = timeFormat(null, 'yyyy-mm-dd hh:MM:ss')
      let year = date.getFullYear()
      let params = {
        appKey: '6738b790-69e8-11ec-a673-6805cab640z1',
        accessCode: 'b03be941dc27498ebcc46311330ee97d',
        keyType: '3',
        numPrefix: 'SPJF-' + this.JFData[0].VILLAGE, //SPJF-村级citycode
        year: year //当前年份
      }
      const res = await numGenerateGetNum(params)
      if (res.data.code == 200) {
        this.JFData[0].lsh = res.data.data
        this.JFData[0].ROW_GUID = uuidv4()
        //有了流水号在往下发
        let num = 0
        console.log(this.JFData)
        this.JFData.forEach((item) => {
          item.TO_ROW_GUID = window.sessionStorage.getItem('uuidv4')
        })
        this.JFData.forEach((item) => {
          for (let i in item) {
            if (item[i] === '' || item[i] == null) {
              console.log(item[i])
              num++
            }
          }
        })
        if (num) {
          this.$toast.fail('请填写必填项')
          return
        }
        this.JFDataAll.push(...this.JFData)
        this.$emit('update:JFData', this.JFData)
        this.$emit('saveLjrData', this.JFDataAll)
        console.log(this.JFDataAll, 'qdbnujibdfuubu')
        this.saveLjrData(this.JFData)
        if (data == '完成') {
          // 格式化数据
          this.JFData.forEach((item) => {
            for (let i in item) {
              item[i] = ''
              if (i == 'TPYE') {
                item[i] = 1
              } else if (i == 'ISDELETE') {
                item[i] = 0
              }
            }
          })
          this.isShow111 = false
        }
      } else {
        //获取流水号失败不允许往下发
        this.$toast.fail('获取流水号失败')
      }
    },
    async hadleFinsh(msg) {
      //不管点那个都要判断必填项

      this.getLSH(msg)
      // if(msg == '完成'){
      //     //关闭并保存
      //     this.getLSH(msg)
      // }else if(msg == '提交并继续添加'){
      //     //清除当前数据并保存
      //     this.JFData.forEach((item)=>{
      //         for(let i in item){
      //             item[i] = ''
      //         }
      //     })
      //     this.JFDataAll.push(...this.JFData)
      // }
    },
    /**
     * 保存数据
     */
    async saveLjrData(data) {
      //调用接口保存数据
      console.log(data)
      data.forEach((item) => {
        item.TO_ROW_GUID = window.sessionStorage.getItem('uuidv4')
        delete item.PKID
      })
      const res = await GSA_Add('63fcfa26-69fa-481e-9383-83cf81793905', true, ...data)
      this.getTableData()
    },
    //删除数据
    async deletJfData() {
      let params = {
        accessCode: '77ad6c1591e54d4fbc45ecc90374a2a210',
        cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000',
        params: {
          dataguid: '63fcfa26-69fa-481e-9383-83cf81793905',
          strParams: JSON.stringify([{ PKID: this.multipleSelection111[0].PKID }]),
          token: window.localStorage.getItem('token')
        }
      }
      const res = await FileServerProxy(params)
      if (JSON.parse(res.data.data).Success) {
        this.getTableData()
      }
    },
    async handleAddusers11(data) {
      if (data == this.button1[0]) {
        this.tableAdd1kes += 1
        this.isShow111 = true
      } else {
        this.$confirm({
          title: '提示',
          message: '确认删吗？',
          btn: {
            okText: '确定',
            cancelText: '取消'
          }
        })
          .then(() => {
            // do something
            this.deletJfData()
          })
          .catch(() => {
            console.log('no')
          })
      }
    },
    //获取数据
    async getTableData() {
      let data = [{ field: 'TO_ROW_GUID', value: window.sessionStorage.getItem('uuidv4') }]
      const res = await GSA_Search('63fcfa26-69fa-481e-9383-83cf81793905', data)
      //   this.JFDataAll.forEach((item) => {
      //     res.Data.forEach((it) => {
      //       for (let i in item) {
      //         for (let j in it) {
      //           if (i == j) {
      //             item[i] = it[j]
      //           }
      //         }
      //       }
      //     })
      //   })
      this.JFDataAll = res.Data
    }
  },
  components: {
    inputSelect
  },
  props: {
    contentData: {
      typeof: Array,
      default: function () {
        return [
          {
            label: '踏勘意见',
            key: 'XCSJ1', //绑定的字段
            ISinput: true, //true:输入框
            required: true, //true: 必填项
            readonly: true, //true:可以填写 false：不可以填写
            isTime: false //true:可以时间选择器 false：下拉框
          },
          {
            label: '是否需要再次踏勘',
            key: 'XCSJ2',
            ISinput: false,
            required: true,
            readonly: true,
            isTime: false,
            emitData: 'a8e5782d-9a59-48e4-b35b-654b9bb667ca',
            NYDARR: []
          },
          {
            label: '人员',
            key: 'XCSJ4',
            ISinput: false,
            required: true,
            readonly: true,
            isTime: false,
            emitData: '2c478392-9c1b-40da-b941-41e4b8c9d6d3',
            NYDARR: []
          },
          {
            label: '踏勘时间',
            key: 'XCSJ3',
            required: true,
            ISinput: false,
            readonly: true,
            isTime: true
          },
          {
            label: '文件名称',
            key: 'XCSJ3',
            required: true,
            ISinput: false,
            readonly: true,
            isTime: true
          }
        ]
      }
    },
    title: {
      typeof: String,
      default: '测试'
    },
    disable: {
      typeof: String,
      default: ''
    },
    ISBianji: {
      type: Boolean,
      default: true
    },
    contentData1: {
      typeof: Array,
      default: function () {
        return [
          {
            label: '踏勘意见',
            key: 'XCSJ1' //绑定的字段
          },
          {
            label: '是否需要再次踏勘',
            key: 'XCSJ2'
          },
          {
            label: '人员',
            key: 'XCSJ4'
          },
          {
            label: '踏勘时间',
            key: 'XCSJ3'
          },
          {
            label: '文件名称',
            key: 'XCSJ3'
          }
        ]
      }
    },
    button1: {
      typeof: Array,
      default: function () {
        return ['添加意见', '删除意见']
      }
    },
    JFData: {
      typeof: Array,
      default: function () {
        return [
          {
            XCSJ1: '1',
            XCSJ2: '1',
            XCSJ3: '1',
            XCSJ4: '1',
            SSS: '1',
            SSX: '1',
            SSZ: '1',
            SSC: '1'
          }
        ]
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tableAdd1 .van-field__label {
  color: black;
  font-weight: bold !important;
  font-size: 13px !important;
  width: 8em;
}

.tableAdd1 .van-field__control {
  text-align: right;
}

.tableAdd1 .van-radio--horizontal {
  margin-right: 30px;
}

.tableAdd1 .van-collapse-item__content {
  padding: 6px;
  color: black;
}

.tableAdd1 .footer1 {
  height: 6vh;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.tableAdd1 .el-table th.el-table__cell > .cell {
  color: black;
}

.tableAdd1 .subTitle {
  width: 50%;
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  position: relative;
  display: flex;
  align-items: center;
  // border-bottom: 1px solid #9b9b9b;
  padding-left: 10px;

  .line {
    width: 6px;
    height: 26px;
    background: #3f8ffc;
    display: inline-block;
    margin-right: 10px;
  }
}

.tableAdd1 .footerButton {
  width: 50%;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.van-action-sheet__header {
  color: black;
  font-weight: bold;
}
</style>
